import {getWeChatOpenIdData,saveWeChatUserData,getDewxbizdataData,bindWechatPhoneNumberData,safeUserData,getUserInfoData,outLoginData,updateUserData,existCellphoneData,bindCellhoneData} from "../../api/user";
export default{
    namespaced:true,
    state:{
        uid:uni.getStorageSync('uid')?uni.getStorageSync('uid'):"",//会员的唯一表示
        token:uni.getStorageSync('token')?uni.getStorageSync('token'):"", //会员登录验证的token
        openid:uni.getStorageSync('openid')?uni.getStorageSync('openid'):"", //微信用户登录的唯一标识
        isLogin:uni.getStorageSync('isLogin')?Boolean(uni.getStorageSync('isLogin')):false, //会员是否录
        userInfo:{}//用户信息
    },
    mutations:{
        ["LOGIN"](state,payload){
            state.uid=payload.uid;
            state.token=payload.token;
            state.openid=payload.openid;
            state.isLogin=true;
            uni.setStorageSync('uid', state.uid);
            uni.setStorageSync('token', state.token);
            uni.setStorageSync('openid', state.openid);
            uni.setStorageSync('isLogin', true);
        },
        //退出会员
        ["OUT_LOGIN"](state,payload){
            state.uid="";
            state.token="";
            state.openid="";
            state.isLogin=false;
            uni.removeStorageSync('uid');
            uni.removeStorageSync('token');
            uni.removeStorageSync('isLogin');
            uni.removeStorageSync('openid');
        },
        //设置用户信息
        ["SET_USER_INFO"](state,payload){
            state.userInfo=payload.userInfo;
        }
    },
    actions:{
        //获取微信小程序用户的openId
        getWeChatOpenId(conText,payload){
            return getWeChatOpenIdData(payload).then(res=>{
                return res;
            })
        },
        //保存用户信息
        saveWeChatUser(conText,payload){
            return saveWeChatUserData(payload).then(res=>{
                return res;
            })
        },
        //微信小程序解密获取手机号
        getDewxbizdata(conText,payload){
            return getDewxbizdataData(payload).then(res=>{
                return res;
            })
        },
        //保存手机号到数据库
        bindWechatPhoneNumber(conText,payload){
            return bindWechatPhoneNumberData({uid:conText.rootState.user.uid,token:conText.rootState.user.token,platform:conText.rootState.system.platform,...payload}).then(res=>{
                return res;
            })
        },
        //会员token认证
        safeUser(conText,payload){
            safeUserData({uid:conText.rootState.user.uid,token:conText.rootState.user.token,platform:conText.rootState.system.platform}).then(res=>{
                if(payload.success){
                    payload.success(res);
                }
            })
        },
        //获取用户信息
        getUserInfo(conText,payload){
            getUserInfoData({uid:conText.rootState.user.uid,token:conText.rootState.user.token,platform:conText.rootState.system.platform,...payload}).then(res=>{
                if(res.code==200){
                    conText.commit("SET_USER_INFO",{userInfo:res.data});
                    if(payload.success){
                        payload.success(res.data);
                    }
                }else{
                    conText.commit("SET_USER_INFO",{userInfo:{}});
                }
            })
        },
        //安全退出
        outLogin(conText,payload){
            outLoginData({uid:conText.rootState.user.uid,platform:conText.rootState.system.platform}).then(res=>{
                if(res.code==200){
                    conText.commit("OUT_LOGIN");
                }
            })
        },
        //修改会员信息
        updateUser(conText,payload){
            updateUserData({uid:conText.rootState.user.uid,token:conText.rootState.user.token,platform:conText.rootState.system.platform,...payload}).then(res=>{
                if(res.code==200){
                    if(payload.success){
                        payload.success();
                    }
                }
            })
        },
        //手机号是否存在
        existCellphone(conText,payload){
            return existCellphoneData(payload).then(res=>{
                return res;
            })
        },
        //绑定手机号
        bindCellhone(conText,payload){
            return bindCellhoneData({uid:conText.rootState.user.uid,token:conText.rootState.user.token,platform:conText.rootState.system.platform,...payload}).then(res=>{
                return res;
            })
        }
    }
}